package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattleBestRecord;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattleEndRecord;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattlePlaceData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangBattleEndRecordDaoImpl extends TempDataObjectDao<CrossKuangBattleEndRecord> {

    public static String getCrossKuangBattleEndRecordSql = "select * from `t_c_kuang_battle_end_record` where `weekId`=? and `groupId`=?";

    public Map<Integer, CrossKuangBattleEndRecord> getCrossKuangBattleEndRecordMap(int weekId, int groupId) {
        Map<Integer, CrossKuangBattleEndRecord> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattleEndRecordSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangBattleEndRecord record = parse(rs);
                map.put(record.getRound(), record);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossKuangBattleEndRecordSql = "update `t_c_kuang_battle_end_record` set bestSeizeUser=?,bestKillUser=?,campBestSeizeUserMap=?,campBestKillUserMap=?,endTime=? where weekId=? and groupId=? and round =? ";
    @Override
    protected boolean updateData(CrossKuangBattleEndRecord crossKuangBattleEndRecord) {
        crossKuangBattleEndRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getBestSeizeUser())));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getBestKillUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getCampBestSeizeUserMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getCampBestKillUserMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleEndRecord.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getRound()));
        if (getDb().executeNoneQuery(updateCrossKuangBattleEndRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossKuangBattleEndRecordSql = "insert into `t_c_kuang_battle_end_record`(weekId,groupId,round,bestSeizeUser,bestKillUser,campBestSeizeUserMap,campBestKillUserMap,endTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossKuangBattleEndRecord crossKuangBattleEndRecord) {
        crossKuangBattleEndRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleEndRecord.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getBestSeizeUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getBestKillUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getCampBestSeizeUserMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleEndRecord.getCampBestKillUserMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleEndRecord.getEndTime()));
        if (getDb().executeNoneQuery(insertCrossKuangBattleEndRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossKuangBattleEndRecord parseData(ResultSet rs) throws SQLException {
        CrossKuangBattleEndRecord crossKuangBattleEndRecord = new CrossKuangBattleEndRecord();
        crossKuangBattleEndRecord.setWeekId(rs.getInt("weekId"));
        crossKuangBattleEndRecord.setGroupId(rs.getInt("groupId"));
        crossKuangBattleEndRecord.setRound(rs.getInt("round"));
        String bestSeizeUser = rs.getString("bestSeizeUser");
        if (!StringUtils.isNullOrEmpty(bestSeizeUser)) {
            crossKuangBattleEndRecord.setBestSeizeUser(JSON.parseObject(bestSeizeUser, new TypeReference<CrossKuangBattleBestRecord>() {

            }));
        }
        String bestKillUser = rs.getString("bestKillUser");
        if (!StringUtils.isNullOrEmpty(bestKillUser)) {
            crossKuangBattleEndRecord.setBestKillUser(JSON.parseObject(bestKillUser, new TypeReference<CrossKuangBattleBestRecord>() {

            }));
        }
        String campBestSeizeUserMap = rs.getString("campBestSeizeUserMap");
        if (!StringUtils.isNullOrEmpty(campBestSeizeUserMap)) {
            crossKuangBattleEndRecord.setCampBestSeizeUserMap(JSON.parseObject(campBestSeizeUserMap, new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.kuang.battle.CrossKuangBattleBestRecord>>() {}));
        }
        String campBestKillUserMap = rs.getString("campBestKillUserMap");
        if (!StringUtils.isNullOrEmpty(campBestKillUserMap)) {
            crossKuangBattleEndRecord.setCampBestKillUserMap(JSON.parseObject(campBestKillUserMap, new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.kuang.battle.CrossKuangBattleBestRecord>>() {}));
        }
        crossKuangBattleEndRecord.setEndTime(rs.getLong("endTime"));

        return crossKuangBattleEndRecord;
    }


}
